<?php
namespace English\Controller;
use Think\Controller;
class IndexController extends Controller {
	private $cacheTime;
	private $d_content,$d_category;
	public function __construct()
	{
		parent::__construct();
	}
	//首页
    public function index(){
		$site_index=S('site_index');
		if($site_index)die($site_index);
		$this->_init();
		//网站首页SEO信息
		$seo=$GLOBALS['setting_model']->getSetting(4);
		$this->assign($seo);
		//广告焦点图
		$d_block=D('Plug/Block');
		$adds=$d_block->getSmallBlock(1);
		$this->assign('adds',$adds);

		//标注首页
		$this->assign('index',true);		
		$site_index=$this->fetch();
		S('site_index',$site_index,$this->cacheTime);
		echo $site_index;
    }
	//内容列表页及栏目页
	public function lists()
	{
		$catid=intval($_GET['catid']);
		$html_info=S('site_lists_'.$catid);
		if($html_info)die($html_info);
		$this->_init();
		$catinfo=$this->d_category->where(array('id'=>$catid))->field('name,parentid,modelids,template1')->find();
		$this->assign('name',$catinfo['name']);
		$this->assign('parentid',$catinfo['parentid']);
		$this->assign('catid',$catid);
		$this->assign('modelid',trim($catinfo['modelids'],','));
		$this->assign('pname',$this->d_category->getName($catinfo['parentid']));
		//获取seo信息
		$seo=$this->d_category->getSeo($catid);
		$this->assign($seo);	
		$html_info=$this->fetch('Lists/'.$catinfo['template1']);
		S('site_lists_'.$catid,$html_info,$this->cacheTime);
		echo $html_info;
	}
	//内容详细页
	public function detail()
	{
		$catid=intval($_GET['catid']);
		$id=intval($_GET['id']);//文章id
		$html_info=S('site_lists_'.$catid.'_'.$id);
		if($html_info)die($html_info);
		$this->_init();		
		//栏目信息
		$catinfo=$this->d_category->where(array('id'=>$catid))->field('name,parentid,modelids,template2')->find();
		$this->assign('name',$catinfo['name']);
		$this->assign('parentid',$catinfo['parentid']);
		$this->assign('catid',$catid);
		$this->assign('pname',$this->d_category->getName($catinfo['parentid']));
		//文章信息
		$modelid=trim($catinfo['modelids'],',');
		$info=$this->d_content->getDetail($modelid,$id,$catid,true);
		$this->assign('info',$info);
		$this->assign('modelid',$modelid);
		$html_info=$this->fetch('Detail/'.$catinfo['template2']);
		S('site_lists_'.$catid.'_'.$id,$html_info,$this->cacheTime);
		echo $html_info;
	}
	//服务支持
	public function service()
	{
		$catid=intval($_GET['catid']);
		$id=intval($_GET['id']);//文章id
		$html_info=S('site_lists_'.$catid.'_'.$id);
		if($html_info)die($html_info);
		$this->_init();		
		//栏目信息
		$catinfo=$this->d_category->where(array('id'=>$catid))->field('name,parentid,modelids,template2')->find();
		$this->assign('name',$catinfo['name']);
		$this->assign('parentid',$catinfo['parentid']);
		$this->assign('catid',$catid);
		$this->assign('pname',$this->d_category->getName($catinfo['parentid']));
		//文章信息
		$modelid=trim($catinfo['modelids'],',');
		$info=$this->d_content->getDetail($modelid,$id,$catid,true);
		$this->assign('info',$info);
		$this->assign('modelid',$modelid);
		$html_info=$this->fetch('Index/'.$catinfo['template2']);
		S('site_lists_'.$catid.'_'.$id,$html_info,$this->cacheTime);
		echo $html_info;
	}	
	function product()
	{
		$id=intval($_GET['id']);//产品id
		$catid=intval($_GET['catid']);
		$html_info=S('product_detail');
		if($html_info)die($html_info);
		$this->_init();
		//栏目信息
		$catinfo=$this->d_category->where(array('id'=>$catid))->field('name,parentid,modelids,template2')->find();
		$this->assign('name',$catinfo['name']);
		if($this->browser() =='mobile'){
			//移动端
			$html_info=$this->fetch('Index/'.'m_'.$catinfo['template2']);
		}else{
			//pc端
			$html_info=$this->fetch('Index/'.$catinfo['template2']);
		}
		S('news_detail',$html_info,$this->cacheTime);
		echo $html_info;
	}	
	function doing()
	{
		$html_info=S('index_doing');
		if($html_info)die($html_info);
		$this->_init();
		$html_info=$this->fetch('Index/doing');
		S('index_doing',$html_info,$this->cacheTime);
		echo $html_info;
	}	
	function login()
	{
		$html_info=S('index_login');
		if($html_info)die($html_info);
		$this->_init();
		$this->assign('name','经销商登录');
		$html_info=$this->fetch('Index/login');
		S('index_login',$html_info,$this->cacheTime);
		echo $html_info;		
	}	
	//初始化网站
	private function _init()
	{
		$site_info=$GLOBALS['setting_model']->getSetting(5);
		$this->cacheTime=$site_info['cachetime'];
		$this->assign('siteinfo',$site_info);
		if(!$this->d_content)
		{
			$this->d_content=D('Content/content');
			$this->assign('d_content',$this->d_content);			
		}
		if(!$this->d_category)
		{
			$this->d_category=D('Content/category');
			$this->assign('d_category',$this->d_category);			
		}
		$navs=M('content_nav')->where('status=1')->select();	
		$this->assign('navs',$navs);
		$this->cacheTime=intval(C('cacheTime'));	
	}

	/*经销商网点*/
	public function network(){
		$html_info=S('index_network');
		if($html_info)die($html_info);
		$this->_init();
		$this->assign('name','经销商地图');
		if($this->browser() =='mobile'){
			//移动端
			$html_info=$this->fetch('Index/m_network');
		}else{
			//pc端
			$html_info=$this->fetch('Index/network');
		}
		S('index_network',$html_info,$this->cacheTime);
		echo $html_info;
	}


	/*获取城市*/
	public function getcity(){
		if(IS_GET){
			$where['cityUpId'] = $_GET['province_id'];
			$citys = M('city')->field('cityId,cityName')->where($where)->select();
			$html = '<option value="" disabled selected>市</option>';
			foreach ($citys as $value) {
				$html .= '<option value="'.$value['cityid'].'">'.$value['cityname'].'</option>';
			}
			exit($html);
		}else{
			$this->error('请选择省份！');
		}
	}

	public function getshops(){
		if(IS_GET){
			$where['province_id'] = $_GET['province_id'];
			if($_GET['city_id']){
				$where['city_id'] = $_GET['city_id'];
			}
			
			
			$shops = M('shopmap')->where($where)->select();
			if($shops){				
				foreach ($shops as $value) {
					$html .='<a href="/Home/Index/getshop?id='.$value['id'].'" Onclick="return getshop('.$value['id'].');" style="color:#30353a;padding:5px;display: inherit;margin:5px;float:left;">'.$value['name'].'</a>';					
				}
				exit($html);
			}else{
				exit('暂无网点信息');
			}	
		}
	}

	public function getshop(){
		if(IS_GET){
			$where['id'] = $_GET['id'];	
			$shop = M('shopmap')->where($where)->find();
			if($shop){		
				$html ='<div class="map-desc-title">
							<span class="name">'.$shop['name'].'</span>
							<div id="imgclose"><img class="close" src="/assets/images/network/close.png"/></div>
						</div>
						<ul>
							<li>网点联系人：<span class="contact">'.$shop['leader'].'</span></li>
							<li>网点电话：<span class="phone">'.$shop['phone'].'</span></li>
							<li>网点地址：<span class="address">'.$shop['address'].'</span></li>
						</ul>';
				exit($html);
			}
		}
	}


	/*手机*/
	function browser(){

		switch(TRUE){

		// Apple/iPhone browser renders as mobile

		case (preg_match('/(apple|iphone|ipod)/i', $_SERVER['HTTP_USER_AGENT']) && preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT'])):

		$browser = "mobile";

		break; 

		// Other mobile browsers render as mobile

		case (preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|

		treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])):

		$browser = "mobile";

		break; 

		// Wap browser

		case (((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'text/vnd.wap.wml') > 0) || (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)) || ((isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])))):

		$browser = "mobile";

		break; 

		// Shortend user agents

		case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,3)),array('lg '=>'lg ','lg-'=>'lg-','lg_'=>'lg_','lge'=>'lge'))); 

		$browser = "mobile";

		break; 

		// More shortend user agents

		case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)),array('acs-'=>'acs-','amoi'=>'amoi','doco'=>'doco','eric'=>'eric','huaw'=>'huaw','lct_'=>'lct_','leno'=>'leno','mobi'=>'mobi','mot-'=>'mot-','moto'=>'moto','nec-'=>'nec-','phil'=>'phil','sams'=>'sams','sch-'=>'sch-','shar'=>'shar','sie-'=>'sie-','wap_'=>'wap_','zte-'=>'zte-')));

		$browser = "mobile";

		break; 

		// Render mobile site for mobile search engines

		case (preg_match('/Googlebot-Mobile/i', $_SERVER['HTTP_USER_AGENT']) || preg_match('/YahooSeeker\/M1A1-R2D2/i', $_SERVER['HTTP_USER_AGENT'])):

		$browser = "mobile";

		break;

		}

		return $browser;

	}


	public function getshops2(){
		if(IS_GET){
			$where['province_id'] = $_GET['province_id'];
			if($_GET['city_id']){
				$where['city_id'] = $_GET['city_id'];
			}
			
			
			$shops = M('shopmap')->where($where)->select();
			if($shops){				
				foreach ($shops as $value) {
					$html .='<div class="itemlist">
								<p>网点名称：<span>'.$value['name'].'</span></p>
								<p>网点负责人：<span>'.$value['leader'].'</span></p>
								<p>网点电话：<span>'.$value['phone'].'</span></p>
								<p>网点地址：<span>'.$value['address'].'</span></p>
							</div>';					
				}
				exit($html);
			}else{
				exit('暂无网点信息');
			}	
		}
	}

}